<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform  ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr,errorModal} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				newProductIdCacheKey: 'qam-wgjy-productInspection-title3-newProductIdCacheKey',
				unitCache: 'qam-wgjy-productInspection-title3-unitCacheKey',
				otherNewProductDatas: [],
				incomingDate: '',
				productTeamId: '',
				productInstId: '',
				bomShowCache:'qam-wgjy-productInspection-title3-bomShow-cacheKey'
			})
		},
		methods:{
			verifyBomShowVersion(){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(!formdata.newProductId){
					let title1 = _that.$refs.autoform.getEditorTitle("title3","newProductId")
					_that.om.toast(title1+'需优先完成编辑！')
					return false
				}else{
					_that.$refs.autoform.changeEditor("title3","bomShowId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								productShowId: formdata.newProductId,
								firstId: formdata.bomShowId
							}),
							url:'bomShow/getBomShowByProductShowPage',
							namefield:'identificationCode',
							notefield:'versionDesc',
							initload:true,
							cachename:_that.bomShowCache
						}
					})
					return true
				}
			},
			getAccordingProductHaveBom(id){
				let _that = this
				_that.om.request(
					'bomShow/getAccordingProductHaveBom',
					{
						productShowId: id
					}
				).then(res=>{
					if(res.succeed){
						if(res.value.bomShowResponse){
							_that.$refs.autoform.formdata.bomShowId = res.value.bomShowResponse.bomShowId
							_that.$refs.autoform.formdata.bomShowVersionNumber = res.value.bomShowResponse.identificationCode
							_that.$refs.autoform.formdata.productSignCode = res.value.bomShowResponse.productSignCode
						}
					}
				})
			},
			initLoad(){
				let _that = this
				let pro1 = _that.om.request('dictSource/loadCombo', { sourceId: 283,categoryId: 6431 });
				let pro2 = _that.om.request('dictSource/loadCombo', { sourceId: 283,categoryId: 6175 });
				let pro3 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 7488 });
				let pro4 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 8333 });
				let pro5 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 8338 });
				let pro6 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 7492 });
				let pro7 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 7489 });
				let pro8 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 7521 });
				let pro9 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 7510 });
				let pro10 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 7505 });
				let pro11 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 9009 });
				let pro12 = _that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 9004 });
				Promise.all([pro1,pro2,pro3,pro4,pro5,pro6,pro7,pro8,pro9,pro10,pro11,pro12]).then(res=>{
					_that.$refs.autoform.changeEditor("title3", "whetherExemptionInspectionId", "data", configSelectData(res[0],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "whetherExemptionInspectionId")
					_that.$refs.autoform.changeEditor("title3", "batchInspectionConclusionId", "data", configSelectData(res[1],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "batchInspectionConclusionId")
					_that.$refs.autoform.changeEditor("title3", "inspectionSchemeId", "data", configSelectData(res[2],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "inspectionSchemeId")
					_that.$refs.autoform.changeEditor("title3", "inspectionMethodId", "data", configSelectData(res[3],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "inspectionMethodId")
					_that.$refs.autoform.changeEditor("title3", "testRulesId", "data", configSelectData(res[4],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "testRulesId")
					_that.$refs.autoform.changeEditor("title3", "inspectionLevelId", "data", configSelectData(res[5],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "inspectionLevelId")
					_that.$refs.autoform.changeEditor("title3", "inspectionStandardTypeId", "data", configSelectData(res[6],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "inspectionStandardTypeId")
					_that.$refs.autoform.changeEditor("title3", "samplingStandardId", "data", configSelectData(res[7],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "samplingStandardId")
					_that.$refs.autoform.changeEditor("title3", "disposalOfNonconformitiesId", "data", configSelectData(res[8],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "disposalOfNonconformitiesId")
					_that.$refs.autoform.changeEditor("title3", "batchUnqualifiedId", "data", configSelectData(res[9],"id","text"))
					_that.$refs.autoform.initSelectIndex("title3", "batchUnqualifiedId")
					_that.$refs.autoform.changeEditor("title3","unqualifiedPhenomenonCategoryId","data",configSelectData(res[10],'id','text'))
					_that.$refs.autoform.initSelectIndex("title3","unqualifiedPhenomenonCategoryId")
					_that.$refs.autoform.changeEditor("title3","unqualifiedFindLocationId","data",configSelectData(res[11],'id','text'))
					_that.$refs.autoform.initSelectIndex("title3","unqualifiedFindLocationId")
				})
			},
			clearNewProductId(){
				let _that = this
				_that.$refs.autoform.clear('title3', 'newProductId');
				_that.$refs.autoform.clear('title3', 'productCode');
				_that.$refs.autoform.clear('title3', 'newProductName');
				_that.$refs.autoform.clear('title3', 'newProductStd');
				_that.$refs.autoform.clear('title3', 'unitId');
				_that.$refs.autoform.clear('title3', 'englishName');
				_that.$refs.autoform.clear('title3', 'commonName');
				_that.$refs.autoform.clear('title3', 'whetherExemptionInspectionId');
				_that.$refs.autoform.clear('title3', 'actualArrivalQuantity');
				_that.$refs.autoform.clear('title3', 'actualArrivalDate');
				_that.$refs.autoform.clear('title3', 'damageNumber');
				_that.$refs.autoform.clear('title3', 'batchInspectionConclusionId');
				_that.$refs.autoform.clear('title3', 'inspectionRecordCode');
				_that.$refs.autoform.clear('title3', 'remark');
				_that.$refs.autoform.clear('title3', 'keyFlagId');
				_that.$refs.autoform.clear('title3', 'productDate');
				_that.$refs.autoform.clear('title3', 'batchUnqualifiedId');
				_that.$refs.autoform.clear('title3', 'disposalOfNonconformitiesId');
				_that.$refs.autoform.clear('title3', 'unqualifiedPhenomenonCategoryId');
				_that.$refs.autoform.clear('title3', 'unqualifiedFindLocationId');
				_that.$refs.autoform.clear('title3', 'defectDescription');
				_that.$refs.autoform.changeEditor("title3","unqualifiedFindLocationId","type","labelbox")
				_that.$refs.autoform.changeEditor("title3","defectDescription","type","labelbox")
				_that.$refs.autoform.clear('title3', 'productUnqualifiedQuality');
			},
			onInputDamageNumber(num){
				let _that = this;
				let znum = Number(_that.$refs.autoform.formdata.quantity) || 0;
				let num2 = Number(_that.$refs.autoform.formdata.qualifiedDamageNumbe) || 0;
				if(znum>0 && Number(num) + num2 > znum){
					errorModal(_that,'提示信息','样本破坏数加合格品破坏数不能大于数量'+znum,()=>{
						_that.$refs.autoform.clear('title3', 'damageNumber');
					})
				}
			},
			onInputQualifiedDamageNumber(num){
				let _that = this;
				let znum = Number(_that.$refs.autoform.formdata.quantity) || 0;
				let num2 = Number(_that.$refs.autoform.formdata.damageNumber) || 0;
				if(znum>0 && Number(num) + num2 > znum){
					errorModal(_that,'提示信息','样本破坏数加合格品破坏数不能大于数量'+znum,()=>{
						_that.$refs.autoform.clear('title3', 'qualifiedDamageNumbe');
					})
				}
			},
			onselectProductDate(data){
				let _that = this
				if (new Date(data).getTime() > new Date(_that.incomingDate).getTime()) {
					errorModal(_that,'提示信息',"生产日期不能晚于检验日期",()=>{
						_that.$refs.autoform.clear("title3",'productDate')
					})
				}
			},
			onSelectDisposalOfNonconformities(item){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.batchInspectionConclusionId == '6226'){
					if(item.code == '40771'){
						if(formdata.damageNumber){
							formdata.qualifiedInspectionNumber = Number(formdata.actualArrivalQuantity) - Number(formdata.damageNumber)
						}else{
							formdata.qualifiedInspectionNumber = Number(formdata.actualArrivalQuantity)
						}
					}
				}
			},
			onInputQualifiedInspection(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(Number(val) > (Number(formdata.actualArrivalQuantity)-Number(formdata.damageNumber))){
					_that.$refs.autoform.clear('title3',"qualifiedInspectionNumber")
					let title1 = _that.$refs.autoform.getEditorTitle("title3","qualifiedInspectionNumber")
					let title2 = _that.$refs.autoform.getEditorTitle("title3","actualArrivalQuantity")
					let title3 = _that.$refs.autoform.getEditorTitle("title3","damageNumber")
					
					_that.om.toast(title1 + '不能大于'+ title2 +'-'+title3)
				}
			},
			
			clearBatchInspectionConclusion(){
				let _that = this
				_that.$refs.autoform.clear('title3', 'batchInspectionConclusionId');
				_that.$refs.autoform.clear('title3', 'batchUnqualifiedId');
				_that.$refs.autoform.clear('title3', 'disposalOfNonconformitiesId');
				_that.$refs.autoform.clear('title3', 'unqualifiedPhenomenonCategoryId');
				_that.$refs.autoform.clear('title3', 'unqualifiedFindLocationId');
				_that.$refs.autoform.clear('title3', 'defectDescription');
				_that.$refs.autoform.changeEditor("title3","unqualifiedFindLocationId","type","labelbox")
				_that.$refs.autoform.changeEditor("title3","defectDescription","type","labelbox")
			},
			changeActualSample(num){
				let _that = this;
				let znum = Number(_that.$refs.autoform.formdata.quantity) || 0;
				let znum2 = Number(_that.$refs.autoform.formdata.unqualifiedNumber) || 0;
				if(Number(num) > znum){
					errorModal(_that,'提示信息','抽样数量不能大于数量'+znum,()=>{
						_that.$refs.autoform.clear('title3', 'actualSample');
					})
				}else{
					_that.$refs.autoform.formdata.defectRatio = (parseFloat((znum2/Number(num)).toFixed(2))*100).toFixed(0) || 0
					if(_that.$refs.autoform.formdata.defectRatio == 'NaN'){
						_that.$refs.autoform.formdata.defectRatio = ''
					}
				}
			},
			changeUnqualifiedNumber(num){
				let _that = this;
				let znum = Number(_that.$refs.autoform.formdata.actualSample) || 0;
				if(Number(num) > znum){
					errorModal(_that,'提示信息','样本不合格数不能大于抽样数量'+znum,()=>{
						_that.$refs.autoform.clear('title3', 'unqualifiedNumber');
					})
				}else{
					_that.$refs.autoform.formdata.defectRatio = (parseFloat((Number(num)/znum).toFixed(2))*100).toFixed(0) || 0
					if(_that.$refs.autoform.formdata.defectRatio == 'NaN'){
						_that.$refs.autoform.formdata.defectRatio = ''
					}
				}
			},
			changeQuantity(num){
				let _that = this;
				if(!_that.$refs.autoform.formdata.unitName || _that.$refs.autoform.formdata.unitName == "PCS"){
					num = Math.round(Number(num))
					_that.$refs.autoform.formdata.quantity = num
				}
				
				let znum = Number(_that.$refs.autoform.formdata.actualSample) || 0;
				if(Number(num) < znum){
					errorModal(_that,'提示信息','抽样数量不能大于数量'+znum,()=>{
						_that.$refs.autoform.clear('title3', 'quantity');
					})
				}
			},
			clearUnqualifiedNumber(){
				let _that = this;
				_that.$refs.autoform.clear('title3', 'unqualifiedNumber');
				_that.$refs.autoform.clear('title3', 'defectRatio');
			},
			clearActualSample(){
				let _that = this;
				_that.$refs.autoform.clear('title3', 'actualSample');
				_that.$refs.autoform.clear('title3', 'defectRatio');
			},
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.newProductIdCacheKey,
					success:(res)=>{
						if(res.data){ 
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							let num = 0;
							formdata.whetherExemptionInspectionId = d.qualityInspection
							formdata.whetherExemptionInspectionName = d.qualityInspectionName
							_that.$refs.autoform.initSelectIndex("title3","whetherExemptionInspectionId")
							for(let i=0; i<_that.otherNewProductDatas.length;i++){
								if(_that.otherNewProductDatas[i].newProductId == d.id){
									num = 1
								}
							}
							
							if(num == 1){
								errorModal(_that,'提示信息','物料编码:'+d.newProductCode+'已存在，请重新选择！',()=>{
									_that.clearNewProductId()
								})
							}else{
								formdata.newProductId = d.id
								_that.getAccordingProductHaveBom(d.id)
								formdata.newProductCode = d.newProductCode
								formdata.productCode = d.productCode
								formdata.newProductName = d.projectName
								formdata.newProductStd = d.projectStd
								formdata.unitId = d.unitId
								formdata.unitName = d.unitName
								formdata.englishName = d.englishName
								formdata.commonName = d.commonName
								formdata.keyFlagId = d.keyFlag
								formdata.keyFlagStr = d.keyFlagStr
								_that.om.request(
									'productInspection/getProductInstQuality',
									{
										productTeamId: _that.productTeamId,
										productInstId: _that.productInstId,
										newProductId: d.id
									}
								,'post').then(res=>{
									formdata.productUnqualifiedQuality = res.value
								})
							}
							uni.removeStorage({
								key:_that.newProductIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.unitCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.unitId = d.unitId
							formdata.unitName = d.unitName
							uni.removeStorage({
								key:_that.unitCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.bomShowCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.bomShowId = d.bomShowId
							formdata.bomShowVersionNumber = d.identificationCode
							uni.removeStorage({
								key:_that.bomShowCache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			_that.incomingDate = e.incomingDate
			_that.productTeamId = e.productTeamId
			_that.productInstId = e.productInstId
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							
							changeEditor('title3', "newProductId", {
								'link': '../../public/pageSearch/pageSearch',
								"linkParam": {
									url:'productShowSecond/getProductPage',
									namefield: 'newProductCode',
									extrafield:'newProductStd',
									notefield:'newProductName',
									searchfield: "q",
									initload:true,
									cachename: _that.newProductIdCacheKey,
								}
							}, null, _that.autoformdata)
							changeEditor("title3","productDate",'noInitDate',true,_that.autoformdata)
							changeEditor("title3","unitId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({}),		
									url:'uomGroup/selectAllChildGrid',
									initload:true,
									searchfield:'q',
									namefield:'unitName',
									extrafield:'unitCode',
									notefield:'unitGroupName',
									cachename:_that.unitCache
								}
							},null,_that.autoformdata)
							
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileSaveNameField", "fileSaveName")
							changeEditor(_that.autoformdata[0].editors, "fileList", "uploadUrl", "uploadFileData/uploadInterimFile")
							changeEditor(_that.autoformdata[0].editors, "fileList", "loadUrl", "uploadFileData/getInterimFile")
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", 143)
							if(_that.formdata.fileList) {
								var tempFileList = []
								var fileUrlList = _that.formdata.fileList.map(t=>{
									tempFileList.push(t.fileSaveName)
									return t.fileSaveName
								})
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
								changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
							}
							_that.otherNewProductDatas = _that.datas.filter(e => e.newProductId)

							_that.initLoad()
							
							}
						}
					})
				}
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
